package org.example.mulitithreading.task_denial_strategy.AbortPolicy;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorDemo01 {
    public static void main(String[] args) {
        // 核心线程数量为1 ， 最大线程池数量为3, 任务容器的容量为1 ,空闲线程的最大存在时间为20s
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 3, 20, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        // 提交5个任务，而该线程池最多可以处理4个任务，当我们使用AbortPolicy这个任务处理策略的时候，就会抛出异常
        for (int x = 0; x < 5; x++) {
            int finalX = x;
            try {
                threadPoolExecutor.submit(() -> System.out.println(Thread.currentThread().getName() + "---->> 执行了任务" + finalX));
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        threadPoolExecutor.shutdown();
    }
}
